import Vue from 'vue'
import VueRouter from 'vue-router'

// 解决重复点击同一路由时报错
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error=> error)
}


// 给软著做的示例文档
// import ruanzhu from '../views/ruanzhu'
// 登陆
import Login from '../views/Login'
// import Webscoket from '../views/websocket'
// 主组件
import Home from '../views/Home'

// 数据上传组件
import dataUpdate from '../components/DataUpdate/dataUpdate'
import dataUpdate_car from '../components/DataUpdate/car'
import dataUpdate_driver from '../components/DataUpdate/driver'
import dataUpdate_wayBill from '../components/DataUpdate/wayBill'
import dataUpdate_price from '../components/DataUpdate/priceList'

// 位置信息组件
import carPosition from "../components/CarPosition/carPosition";
import carPosition_find from "../components/CarPosition/find";

// 车辆组件
import car from '../components/Car/car'
import car_info from '../components/Car/carInfo'

// 排名
import rank from '../components/Rank/driverRank'
import rank_week from '../components/Rank/week'
import rank_month from '../components/Rank/month'

// 轨迹动画
import animate from '../components/Animate/animate'

import NotFound from '../views/NotFound'

Vue.use(VueRouter)

const routes = [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta: {
        title: '登陆',
        enterClick: true
      }
    },
    {
        path: '/Home',
        component: Home,
        meta: {
            title: '无车承运平台'
        },
        children: [
            {
                path: 'dataUpdate',
                component: dataUpdate,
                children: [
                    {
                        path: 'dataUpdate_car',
                        component: dataUpdate_car,
                        meta: {
                            title: '上传车辆信息'
                        },
                    },
                    {
                        path: 'dataUpdate_driver',
                        component: dataUpdate_driver,
                        meta: {
                            title: '上传司机信息'
                        },
                    },
                    {
                        path: 'dataUpdate_wayBill',
                        component: dataUpdate_wayBill,
                        meta: {
                            title: '上传电子订单'
                        },
                    },
                    {
                        path: 'dataUpdate_price',
                        component: dataUpdate_price,
                        meta: {
                            title: '上传资金流水'
                        },
                    }
                ]
            },
            {
                path: 'carPosition',
                component: carPosition,
                children: [
                    {
                        path: 'carPosition_find',
                        component: carPosition_find,
                        meta: {
                            title: '查看车辆位置',
                            enterClick: true
                        }
                    }
                ]
            },
            {
                path: 'car',
                component: car,
                children: [
                    {
                        path: 'car_info',
                        component: car_info,
                        meta: {
                            title: '排队信息查看',
                            enterClick: true
                        }
                    }
                ]
            },
            {
                path: 'rank',
                component: rank,
                children: [
                    {
                        path: 'rank_week',
                        component: rank_week,
                        meta: {
                            title: '周排名'
                        }
                    },
                    {
                        path: 'rank_month',
                        component: rank_month,
                        meta: {
                            title: '月排名'
                        }
                    }
                ]
            },
            {
                path: 'animate',
                component: animate,
                meta: {
                    title: '轨迹动画'
                }
            }
        ]
    },
    {
        path: '*',
        redirect: '/404'
    },
    {
        path: '/404',
        component: NotFound
    }
  // {
  //   path: '/',
  //   name: 'ruanzhu',
  //   component: ruanzhu
  // },
  // {
  //   path: '/',
  //   name: 'Webscoket',
  //   component: Webscoket
  // }
]

const router = new VueRouter({
  // mode: 'history',
  // base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  if (to.path === '/login') {
    next()
  }
  else {
    let token = localStorage.getItem('userToken')
    if (token === null || token === '') {
        alert('令牌已过期 请重新登陆')
        next({
          path: '/login',
          query: {
              redirect: to.fullPath
          }
      })
    } else {
      next()
    }
  }
})


export default router
